package file;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-06-23
 * Time: 9:41
 */
public class Demo14 {
    public static void main(String[] args) {
        try {
            // 按照字节读取文件内容
            InputStream inputStream = new FileInputStream("F:/文件读写测试/test.txt");

            // 按照字符读取文件内容
            Reader reader = new FileReader("F:/文件读写测试/test.txt");

            System.out.println("按照字节进行读取：");
            while (true){
                byte[] b= new byte[1024];
                int len = inputStream.read(b);
                if(len == -1){
                    break;
                }
                for (int i = 0; i < len; i++) {
                    System.out.print(b[i] + " ");
                }
            }
            // 读取完毕，关闭文件，释放资源
            inputStream.close();
            System.out.println("\n按照字符进行读取：");
            while (true){
                char[] cbuf = new char[1024];
                int len = reader.read(cbuf);
                if(len == -1) {
                    break;
                }
                for (int i = 0; i < len; i++) {
                    System.out.print(cbuf[i]+" ");
                }
            }
            reader.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
